﻿Imports System.Net
Imports System.Text
Imports System.IO

Public Enum HttpVerb
    POST
    PUT
    DELETE
    GETS
End Enum
Public Class RestClient
    Dim url As String
    Public Property EndPoint As String    '请求的url地址  eg：   http://215.23.12.45:8080/order/order_id=1&isdel=0
        Get
            Return url
        End Get
        Set(value As String)
            url = value
        End Set
    End Property
    Dim meth As HttpVerb
    Public Property Method As HttpVerb     '请求的方法
        Get
            Return meth
        End Get
        Set(value As HttpVerb)
            meth = value
        End Set
    End Property
    Dim type As String
    Public Property ContentType As String '格式类型：我用的是application/json，具体使用什么，看需求吧
        Get
            Return type
        End Get
        Set(value As String)
            type = value
        End Set
    End Property
    Dim data As String
    Public Property PostData As String    '传送的数据，当然了我使用的是json字符串
        Get
            Return data
        End Get
        Set(value As String)
            data = value
        End Set
    End Property
    Public Sub RestClient()
        EndPoint = ""
        Method = HttpVerb.GETS
        ContentType = "text/xml"
        PostData = ""
    End Sub
    Public Sub RestClient(ByVal endpoints As String)
        EndPoint = endpoints
        Method = HttpVerb.GETS
        ContentType = "text/xml"
        PostData = ""
    End Sub
    Public Sub RestClient(ByVal endpoints As String, ByVal methods As HttpVerb)
        EndPoint = endpoints
        Method = methods
        ContentType = "text/xml"
        PostData = ""
    End Sub
    Public Sub RestClient(ByVal endpoints As String, ByVal methods As HttpVerb, ByVal postDatas As String)
        EndPoint = endpoints
        Method = methods
        ContentType = "text/xml"
        PostData = postDatas
    End Sub
    Public Function MakeRequest() As String
        Return MakeRequest("")
    End Function
    Public Function MakeRequest(ByVal parameters As String) As String

        Dim request As HttpWebRequest = WebRequest.Create(EndPoint + parameters)

        request.Method = Method.ToString()
        request.ContentLength = 0
        request.ContentType = ContentType

        If PostData <> "" And Method = HttpVerb.POST Then
            Dim encodings = New UTF8Encoding()
            Dim bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData) '编码方式按自己需求进行更改，我在项目中使用的是UTF-8
            request.ContentLength = bytes.Length
            Using writeStream = request.GetRequestStream()
                writeStream.Write(bytes, 0, bytes.Length)
            End Using
        End If
        If PostData <> "" And Method = HttpVerb.PUT Then
            Dim encodings = New UTF8Encoding()
            Dim bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData) '编码方式按自己需求进行更改，我在项目中使用的是UTF-8
            request.ContentLength = bytes.Length
            Using writeStream = request.GetRequestStream()
                writeStream.Write(bytes, 0, bytes.Length)
            End Using
        End If
        Using response As HttpWebResponse = request.GetResponse()

            Dim responseValue = String.Empty

            If response.StatusCode <> HttpStatusCode.OK Then
                Dim message = String.Format("Request failed. Received HTTP {0}", response.StatusCode)
                Throw New ApplicationException(message)
            End If

            ' grab the response
            Using responseStream As System.IO.Stream = response.GetResponseStream()

                If responseStream IsNot Nothing Then
                    Using reader As StreamReader = New StreamReader(responseStream)
                        responseValue = reader.ReadToEnd()
                    End Using
                End If
            End Using

            Return responseValue
        End Using
    End Function


    Public Function MakeRequest(parameters As String, timeOut As Integer, header(,) As String, PostData As String) As String
        Dim responseValue As String = String.Empty
        Try
            Dim request As HttpWebRequest = CType(WebRequest.Create(parameters), HttpWebRequest)
            request.Method = HttpVerb.POST
            request.ContentLength = 0
            request.ContentType = "application/json"
            For i As Integer = 0 To header.GetLength(0) - 1
                request.Headers.Add(header(i, 0), header(i, 1))
            Next
            request.Timeout = timeOut
            If String.IsNullOrEmpty(PostData) = False Then
                Dim bytes() As Byte = Encoding.UTF8.GetBytes(PostData)
                request.ContentLength = bytes.Length
                Using writeStream As Stream = request.GetRequestStream()
                    writeStream.Write(bytes, 0, bytes.Length)
                End Using
            End If

            Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

                If response.StatusCode <> HttpStatusCode.OK Then
                    Dim message As String = String.Format("Request failed. Received HTTP {0}", response.StatusCode)
                    Throw New ApplicationException("MES访问异常：" & message)
                End If
                Using responseStream As Stream = response.GetResponseStream()
                    If responseStream IsNot Nothing Then
                        Using reader As StreamReader = New StreamReader(responseStream)
                            responseValue = reader.ReadToEnd()
                        End Using
                    End If
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            responseValue = ""
        End Try
        Return responseValue
    End Function

End Class
